package com.fieldchain.ex.fa100.ui.personal.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.base.MyBaseActivity;
import com.fieldchain.ex.fa100.mvp.presenter.ForgetPresenter;
import com.fieldchain.ex.fa100.mvp.view.ForgetView;

import com.fieldchain.ex.fa100.utils.common.EditTextUtils;
import com.fieldchain.ex.fa100.utils.common.InputAnimation;
import com.fieldchain.ex.fa100.utils.common.VerticalDisUtils;
import com.fieldchain.ex.fa100.utils.net.common.EmailCode;
import com.fieldchain.ex.fa100.utils.net.common.SmsCode;
import com.fieldchain.ex.fa100.utils.string.PEChecked;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/11
 */
public class ForgetActivity extends MyBaseActivity<ForgetPresenter, ForgetView> implements ForgetView, View.OnClickListener {
    private RelativeLayout reaBack;
    private ImageView ivBac;
    private EditText etPhone;
    private RelativeLayout reaImgcode;
    private ImageView ivImgcode;
    private EditText etImagecode;
    private EditText etSmsver;
    private TextView tvVerSms;
    private RelativeLayout reaDis;
    private TextView tvSure;
    private LinearLayout linBottom;


    private InputAnimation animation;
    private EditTextUtils etUtils;

    @Override
    public void findView() {
        reaBack = findViewById(R.id.rea_back);
        ivBac = findViewById(R.id.iv_bac);
        etPhone = findViewById(R.id.et_phone);
        reaImgcode = findViewById(R.id.rea_imgcode);
        ivImgcode = findViewById(R.id.iv_imgcode);
        etImagecode = findViewById(R.id.et_imagecode);
        etSmsver = findViewById(R.id.et_smsver);
        tvVerSms = findViewById(R.id.tv_ver_sms);
        reaDis = findViewById(R.id.rea_dis);
        tvSure = findViewById(R.id.tv_sure);
        linBottom = findViewById(R.id.lin_bottom);
    }

    @Override
    public void setValue() {
        setInput();
        etUtils = new EditTextUtils();
        etUtils.append(etSmsver, reaDis);
        etUtils.start();

        reaBack.setOnClickListener(this);
        ivImgcode.setOnClickListener(this);
        tvVerSms.setOnClickListener(this);
        tvSure.setOnClickListener(this);
        presenter.getVer();
    }

    @Override
    public int getLayout() {
        return R.layout.activity_forget1;
    }

    public void setInput() {
        animation = new InputAnimation();
        animation.attachView((Activity) mContext, 40, etSmsver, linBottom, ivBac);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rea_back:
                finish();
                break;
            case R.id.iv_imgcode:
                presenter.getVer();
                break;
            case R.id.tv_ver_sms:
                String name=etPhone.getText().toString();
                if(PEChecked.isMobileNO(name)){
                    VerticalDisUtils.getInstance().start(etSmsver, tvVerSms, reaDis);
                    SmsCode.getInstance().getSms(name, etImagecode.getText().toString());
                }else {
                    VerticalDisUtils.getInstance().start(etSmsver, tvVerSms, reaDis);
                    EmailCode.getInstance().getEmail(name, etImagecode.getText().toString());
                }
                break;

            case R.id.tv_sure:
                String name2=etPhone.getText().toString();
                String smsCode=etSmsver.getText().toString();
                presenter.forget(smsCode,name2);
                break;
        }
    }

    @Override
    public void forgetResult(int type) {
        if(type==200){
            Intent intent=new Intent(mContext,ForgetNextActivity.class);
            intent.putExtra("imagcode",etImagecode.getText().toString());
            intent.putExtra("smsCode",etSmsver.getText().toString());
            intent.putExtra("name",etPhone.getText().toString());
            startActivity(intent);
            finish();
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        etUtils.cancle();
        animation.detachView();
        animation = null;
        VerticalDisUtils.getInstance().cancle();
    }
    @Override
    public void verImage(Bitmap bit) {
        ivImgcode.setImageBitmap(bit);
    }
}
